package com.atguigu.controller;

import com.atguigu.common.MessageConstant;
import com.atguigu.constant.RedisMessageConstant;
import com.atguigu.util.SMSUtils;
import com.atguigu.util.ValidateCodeUtils;
import com.atguigu.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.JedisPool;

@RestController
@RequestMapping("ValidateCode")
public class ValidateCodeController {

    @Autowired
    private JedisPool jedisPool;
    @RequestMapping("/send4oder")
    public Result send4oder(String telephone){
        Integer code = ValidateCodeUtils.generateValidateCode(4);
        try {
            SMSUtils.sendShortMessage(telephone, code.toString());
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.SEND_VALIDATECODE_FAIL);

        }
        System.out.println("发送的手机验证码为：" + code);
        jedisPool.getResource().setex(telephone+ RedisMessageConstant.SENDTYPE_ORDER,60*5,code.toString());
        return new Result(true,MessageConstant.SEND_VALIDATECODE_SUCCESS);
    }

    @RequestMapping("/send4Login")
    public Result send4Login(String telephone){
        Integer code = ValidateCodeUtils.generateValidateCode(4);
        try {
            SMSUtils.sendShortMessage(telephone, code.toString());
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.SEND_VALIDATECODE_FAIL);

        }
        System.out.println("发送的手机验证码为：" + code);
        jedisPool.getResource().setex(telephone+ RedisMessageConstant.SENDTYPE_LOGIN,60*5,code.toString());
        return new Result(true,MessageConstant.SEND_VALIDATECODE_SUCCESS);
    }

}
